package com.bailugansan.service.implement;

import com.bailugansan.dao.UserDao;
import com.bailugansan.dao.implement.UserDaoImpl;
import com.bailugansan.pojo.Role;
import com.bailugansan.pojo.User;
import com.bailugansan.service.UserService;
import com.bailugansan.util.DbUtil;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author BailuGansan
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserServiceImpl() {
        // 多态实例化 userDao
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) {
        Connection conn = null;
        User user = null;

        conn = DbUtil.getConn();
        // 从 Dao 层获取数据库查询的数据
        user = userDao.login(conn, userCode);
        // 校验密码是否正确
        if(!password.equals(user.getUserPassword())){
            user = null;
        }

        return user;
    }

    @Override
    public boolean pwdModify(int id, String newPassword) {
        Connection conn = null;
        boolean flag = false;

        conn = DbUtil.getConn();

        int updateResult = userDao.pwdModify(conn, id, newPassword);

        if(updateResult == 1){
            flag = true;
        }

        return flag;
    }

    @Override
    public int getUserCount(String userName, int userRole) {
        Connection conn = null;
        int resultCount = 0;

        try {
            conn = DbUtil.getConn();
            resultCount = userDao.getUserCount(conn, userName, userRole);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DbUtil.closeConn(conn, null, null);
        }


        return resultCount;
    }

    @Override
    public List<User> getUserList(String userName, int userRole, int currentPage, int pageSize) {
        Connection conn = null;
        List<User> userList = null;

        try {
            conn = DbUtil.getConn();
            userList = userDao.getUserList(conn,userName, userRole, currentPage, pageSize);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DbUtil.closeConn(conn, null, null);
        }

        return userList;
    }

    @Override
    public List<Role> getRoleList() {
        Connection conn = null;
        List<Role> roleList = null;

        try {
            conn = DbUtil.getConn();
            roleList = userDao.getRoleList(conn);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            DbUtil.closeConn(conn, null, null);
        }

        return roleList;
    }

//    @Test
//    void testGetUserList(){
//        List<User> list = getUserList("赵", 0, 0, 5);
//        for (User user : list) {
//            System.out.println(user.toString());
//        }
//    }

//    @Test
//    void testGetRoleList(){
//        List<Role> roleList = getRoleList();
//        for (Role role : roleList) {
//            System.out.println(role.toString());
//        }
//    }

}
